package com.cloudxingkong.dianyinke.Student.Adapter;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.cloudxingkong.dianyinke.NewTeacher.Utils.WordWrapView;
import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Student.bean.PingFen;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by ningfei on 2016/3/26.
 */
public class PingFenAdapter extends BaseAdapter {
	private List<PingFen.ListBean> data;
	private Context context;

	public PingFenAdapter (List<PingFen.ListBean> data, Context context) {
		this.data = data;
		this.context = context;

	}

	@Override
	public int getCount () {
		return data == null ? 0 : data.size ();
	}

	@Override
	public Object getItem (int position) {
		return data == null ? null : data.get (position);
	}

	@Override
	public long getItemId (int position) {
		return position;
	}

	@Override
	public View getView (int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = View.inflate (context, R.layout.pingfen_item, null);
			holder = new ViewHolder ();
			holder.tv_pf_name = (TextView) convertView.findViewById (R.id.tv_pf_name);
			holder.tv_pf_title = (TextView) convertView.findViewById (R.id.tv_pf_title);
			holder.tv_pf_content = (TextView) convertView.findViewById (R.id.tv_pf_content);
			holder.tv_pf_price = (TextView) convertView.findViewById (R.id.tv_pf_price);
			holder.ll_pingfen = (LinearLayout) convertView.findViewById (R.id.ll_pingfen);
			holder.wwv = ((WordWrapView) convertView.findViewById (R.id.pf_view));
			holder.ratingBar = (RatingBar) convertView.findViewById (R.id.pf_grade);
			holder.pf_icon = (CircleImageView) convertView.findViewById (R.id.pf_iocn);
			convertView.setTag (holder);
		} else {
			holder = (ViewHolder) convertView.getTag ();
		}
		holder.tv_pf_name.setText (data.get (position).getMemname () + "");
		holder.tv_pf_title.setText (data.get (position).getTitle () + "");
		holder.tv_pf_content.setText (data.get (position).getNote () + "");
		holder.tv_pf_price.setText ("￥" + data.get (position).getPrice () + "");
		holder.ratingBar.setRating ((float) data.get (position).getAvgscore ());//评分


		ImageLoader.getInstance ().displayImage (data.get (position).getCoverpic (), holder.pf_icon);

		String label = data.get (position).getLabel ();//标签
		if (label != null) {
			String[] split = label.split (",");
			for (int i = 0; i < split.length; i++) {
				TextView tv = new TextView (context);
				tv.setGravity (Gravity.CENTER);
				tv.setTextColor (context.getResources ().getColor (R.color.main));
				tv.setBackgroundResource (R.drawable.shape_rectangle_white_solid_btn);
				tv.setText (split[i]);
				if (holder.wwv.getChildCount () < split.length) {
					holder.wwv.addView (tv);
				}
				//Log.e("test", position + " -- " + holder.wwv.getChildCount() + " -- " + split.length + " -- " + split[i] + " --- " + tv.getText());
			}
		} else {
			Log.e ("test", "标签为空");
		}
		return convertView;
	}

	private class ViewHolder {
		TextView tv_pf_name;
		TextView tv_pf_title;
		TextView tv_pf_content;
		TextView tv_pf_price;
		RatingBar ratingBar;
		WordWrapView wwv;
		LinearLayout ll_pingfen;
		private CircleImageView pf_icon;
	}
}